package clock

import (
	"fmt"
)

const testVersion = 4

type Clock struct {
	Hour   int
	Minute int
}

func New(hour, minute int) Clock {
	totalMin := (hour*60 + minute) % (24 * 60)

	if totalMin < 0 {
		totalMin = 24*60 + totalMin
	}

	return Clock{totalMin / 60, totalMin % 60}
}

func (c Clock) String() string {
	return fmt.Sprintf("%02d:%02d", c.Hour, c.Minute)
}

func (c Clock) Add(minutes int) Clock {
	originalMin := c.Hour*60 + c.Minute
	totalMin := minutes

	originalMin += totalMin

	if originalMin < 0 {
		originalMin = 24*60 + (originalMin % (24 * 60))
	}

	originalMin = originalMin % (24 * 60)
	c.Hour = originalMin / 60
	c.Minute = originalMin % 60

	return c
}
